package com.mycompany.app.common.extjs;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;

import com.mycompany.app.common.audit.access.filter.model.LoggingMessage;

public class ExtJSUtils {

    public static String wrapJsonReader(String jsonList, Class clas,
            int listSize) {

        StringBuilder sb = new StringBuilder();
        sb
                .append("{'metaData':{totalProperty:'results',root:'rows',id:'id',fields:[");

        Field[] fs = clas.getDeclaredFields();
        for (int i = 0; i < fs.length; i++) {
            if (!fs[i].getName().equals("serialVersionUID")) {
                sb.append("{name:'").append(fs[i].getName()).append("',type:'");
                
                if (fs[i].getType() == Long.class
                        || fs[i].getType() == Integer.class) {
                    sb.append("int'},");
                } else if (fs[i].getType() == Float.class) {
                    sb.append("float'},");
                } else if (fs[i].getType() == String.class) {
                    sb.append("string'},");
                } else if (fs[i].getType() == java.util.Date.class) {
                    sb.append("date'},");
                } else {
                    throw new RuntimeException("unidentified field type:"
                            + fs[i].getName() + "  " + fs[i].getType());
                }
            }
        }
        sb.deleteCharAt(sb.length() - 1);
        sb.append("]},'results':").append(listSize).append(",'rows':").append(
                jsonList).append("}");

        return sb.toString();
    }

    public static void main(String[] args) throws IllegalAccessException,
            InvocationTargetException, NoSuchMethodException {

        // b=BeanUtilsBean.getInstance();
        Field[] fs = LoggingMessage.class.getDeclaredFields();
        for (int i = 0; i < fs.length; i++)
            System.out.println(fs[i].getName()+"  "+fs[i].getType());
        System.out.println(ExtJSUtils.wrapJsonReader("[]",
                LoggingMessage.class, 0));

        // System.out.println(ToStringBuilder.reflectionToString());

    }

}
